package com.puzzlebazar.client.util;

import java.util.Vector;


public class TextUtil {

	/**
	 * Puts the passed string into a standard HTML span with the given 
	 * css class as style
	 * 
	 * @param string The string to put in a span
	 * @param cssClass The CSS style to use
	 * @return The string in a styled span
	 */
	public static String putInSpan( String string, String cssClass, boolean useHTML ) {
		if( !useHTML ) return string;
		return "<span class=\""+cssClass+"\">" + string + "</span>";
	}

	/**
	 * Puts the passed string into a standard HTML action link using javascript
	 * 
	 * @param string The string to put in an action link
	 * @return The string in an action link
	 */
	public static String makeActionLink( String string) {
		return "<a href='javascript:;'>" + string + "</a>";
	}
	
	/**
	 * Converts a single string containing a list into a vector. Leading or trailing
	 * spaces in each element will be trimmed
	 * 
	 * @param stringList The single string containing the elements to split 
	 * @param separator The separator (for example "," to parse comma separated lists)
	 * @return A new vector containing all the elements in the passed string
	 */
	public static Vector<String> split(String stringList, String separator) {
		Vector<String> result = new Vector<String>();
		String[] stringArray = stringList.split(separator);
		for (int i = 0; i < stringArray.length; i++) {
			String trimmed = stringArray[i].trim(); 
			if( trimmed.length() > 0 )
				result.add( trimmed );
		}
		return result;
	}

	/**
	 * Converts a vector of strings to a single string that can be later
	 * reconstructed using split.
	 * 
	 * @param list The list of string to concatenate in a single string
	 * @param separator The separator (for example "," to create comma separated lists)
	 * @return A string where all elements of the passed list are concatenated
	 */
	public static String join(Vector<String> list, String separator) {
		String result = new String();
		boolean isFirst = true;
		for (String string : list) {
			if( isFirst ) 
				isFirst = false;
			else
				result += separator;
			result += string;
		}
		return result;
	}

	/**
	 * Converts a vector of strings to a single string that can be later
	 * reconstructed using split. 
	 * 
	 * @param list The list of string to concatenate in a single string
	 * @param prefix A prefix to put before each element of the list
	 * @param suffix A suffix to put after each element of the list
	 * @param separator The separator (for example "," to create comma separated lists)
	 * @return A string where all elements of the passed list are concatenated
	 */
	public static String join(Vector<String> list, String prefix, String suffix, String separator) {
		String result = new String();
		boolean isFirst = true;
		for (String string : list) {
			if( isFirst ) 
				isFirst = false;
			else
				result += separator;
			result += prefix + string + suffix;
		}
		return result;
	}


	/**
	 * Converts a vector of strings to a single string that can be later
	 * reconstructed using split. 
	 * 
	 * @param completeString The complete string to add to 
	 * @param stringToAdd The string to add
	 * @param separator The separator to put before the string to add (if needed)
	 * @return The extended string 
	 */
	public static String addToString(String completeString, String stringToAdd, String separator) {
		if( !completeString.isEmpty() )
			completeString += separator;
		return completeString + stringToAdd;
	}
	
	
}
